package pin.db.schema.base;
import pin.db.schema.*;
import java.util.Vector;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
public class BlogSpotPeer extends BasePeer{
	public static final String tableName = "BlogSpot";
	public static final String BLOGSPOTID = "blogSpotId";
	public static final String TITLE = "title";
	public static final String DISCRIPTION = "discription";
	public static final String EMAIL = "email";
	public static final String SIGNATURE = "signature";
	public static final String FORWARDPOLICY = "forwardPolicy";
	public static final String PUBLICPOLICY = "publicPolicy";
	public static final String ORDERPOLICY = "orderPolicy";
	public static final String ARCHIVEPOLICY = "archivePolicy";
	public static final String REPLYPOLICY = "replyPolicy";
	public static final String TIMEFORMAT = "timeFormat";
	public static final String BLOGSSHOWEDPERPAGE = "blogsShowedPerPage";
	public static final String BLOGSTYLE = "blogStyle";
	public static final String FEEDBACK = "feedback";
	public static final String STATE = "state";
	public static final String POSTCOUNT = "postCount";
	public static final String SCORE = "score";
	public static final String AVERAGESCORE = "averageScore";
	public static final String LASTPOSTTIME = "lastPostTime";
	public static final String LASTREPLYTIME = "lastReplyTime";
	public static final String CATEGORIES = "categories";
	public static final String[] columnNames=new String[]{"blogSpotId","title","discription","email","signature","forwardPolicy","publicPolicy","orderPolicy","archivePolicy","replyPolicy","timeFormat","blogsShowedPerPage","blogStyle","feedback","state","postCount","score","averageScore","lastPostTime","lastReplyTime","categories"};
	public static BlogSpot getBlogSpotByPKs(Object blogSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotPeer.tableName,BlogSpotPeer.columnNames," TRUE  AND `blogSpotId`=? ",new Object[]{blogSpotId,},0, 1,conn);
			BlogSpot blogSpot = new BlogSpot();
		fill(rs,blogSpot);
		return blogSpot;
	}
	public static BlogSpot getBlogSpotReferenceColumnsOnlyByPKs(Object blogSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotPeer.tableName,new String[]{"blogSpotId",}," TRUE AND `blogSpotId`=?",new Object[]{blogSpotId,}, 0,  1,conn);
			BlogSpot blogSpot = new BlogSpot();
		fill(rs,blogSpot);
		return blogSpot;
	}
	public static BlogSpot getBlogSpotByBlogSpotId(long blogSpotId, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(BlogSpotPeer.tableName,BlogSpotPeer.columnNames,"`blogSpotId`=?",new Object[]{blogSpotId},0, 1,conn);
			BlogSpot blogSpot = new BlogSpot();
		fill(rs,blogSpot);
		return blogSpot;
	}
	public static void fill(ResultSet rs, BlogSpot blogSpot) throws SQLException {
		if(rs.next()){
			try{blogSpot.setBlogSpotId(rs.getLong("blogSpotId"));}catch(Exception ex){}
			try{blogSpot.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{blogSpot.setDiscription(rs.getString("discription"));}catch(Exception ex){}
			try{blogSpot.setEmail(rs.getString("email"));}catch(Exception ex){}
			try{blogSpot.setSignature(rs.getString("signature"));}catch(Exception ex){}
			try{blogSpot.setForwardPolicy(rs.getInt("forwardPolicy"));}catch(Exception ex){}
			try{blogSpot.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			try{blogSpot.setOrderPolicy(rs.getInt("orderPolicy"));}catch(Exception ex){}
			try{blogSpot.setArchivePolicy(rs.getInt("archivePolicy"));}catch(Exception ex){}
			try{blogSpot.setReplyPolicy(rs.getInt("replyPolicy"));}catch(Exception ex){}
			try{blogSpot.setTimeFormat(rs.getInt("timeFormat"));}catch(Exception ex){}
			try{blogSpot.setBlogsShowedPerPage(rs.getInt("blogsShowedPerPage"));}catch(Exception ex){}
			try{blogSpot.setBlogStyle(rs.getInt("blogStyle"));}catch(Exception ex){}
			try{blogSpot.setFeedback(rs.getString("feedback"));}catch(Exception ex){}
			try{blogSpot.setState(rs.getInt("state"));}catch(Exception ex){}
			try{blogSpot.setPostCount(rs.getLong("postCount"));}catch(Exception ex){}
			try{blogSpot.setScore(rs.getInt("score"));}catch(Exception ex){}
			try{blogSpot.setAverageScore(rs.getInt("averageScore"));}catch(Exception ex){}
			try{blogSpot.setLastPostTime(rs.getTimestamp("lastPostTime"));}catch(Exception ex){}
			try{blogSpot.setLastReplyTime(rs.getTimestamp("lastReplyTime"));}catch(Exception ex){}
			try{blogSpot.setCategories(rs.getString("categories"));}catch(Exception ex){}
			blogSpot.resetModified();
		}
	}
	public static void fill(ResultSet rs, Collection<BlogSpot> blogSpotCollection) throws SQLException {
		while(rs.next()){
			BlogSpot blogSpot=new BlogSpot();
			try{blogSpot.setBlogSpotId(rs.getLong("blogSpotId"));}catch(Exception ex){}
			try{blogSpot.setTitle(rs.getString("title"));}catch(Exception ex){}
			try{blogSpot.setDiscription(rs.getString("discription"));}catch(Exception ex){}
			try{blogSpot.setEmail(rs.getString("email"));}catch(Exception ex){}
			try{blogSpot.setSignature(rs.getString("signature"));}catch(Exception ex){}
			try{blogSpot.setForwardPolicy(rs.getInt("forwardPolicy"));}catch(Exception ex){}
			try{blogSpot.setPublicPolicy(rs.getInt("publicPolicy"));}catch(Exception ex){}
			try{blogSpot.setOrderPolicy(rs.getInt("orderPolicy"));}catch(Exception ex){}
			try{blogSpot.setArchivePolicy(rs.getInt("archivePolicy"));}catch(Exception ex){}
			try{blogSpot.setReplyPolicy(rs.getInt("replyPolicy"));}catch(Exception ex){}
			try{blogSpot.setTimeFormat(rs.getInt("timeFormat"));}catch(Exception ex){}
			try{blogSpot.setBlogsShowedPerPage(rs.getInt("blogsShowedPerPage"));}catch(Exception ex){}
			try{blogSpot.setBlogStyle(rs.getInt("blogStyle"));}catch(Exception ex){}
			try{blogSpot.setFeedback(rs.getString("feedback"));}catch(Exception ex){}
			try{blogSpot.setState(rs.getInt("state"));}catch(Exception ex){}
			try{blogSpot.setPostCount(rs.getLong("postCount"));}catch(Exception ex){}
			try{blogSpot.setScore(rs.getInt("score"));}catch(Exception ex){}
			try{blogSpot.setAverageScore(rs.getInt("averageScore"));}catch(Exception ex){}
			try{blogSpot.setLastPostTime(rs.getTimestamp("lastPostTime"));}catch(Exception ex){}
			try{blogSpot.setLastReplyTime(rs.getTimestamp("lastReplyTime"));}catch(Exception ex){}
			try{blogSpot.setCategories(rs.getString("categories"));}catch(Exception ex){}
			blogSpot.resetModified();
			blogSpotCollection.add(blogSpot);
		}
	}
	public static boolean update(BlogSpot blogSpot, Connection conn) throws SQLException {
		Vector<String> columns = new Vector<String>();
		for (int i = 0; i < BlogSpotPeer.columnNames.length; i++) {
			if (((BlogSpotBase)blogSpot).modified[i]) {
				columns.add(BlogSpotPeer.columnNames[i]);
			}
		}
		if (columns.isEmpty()) return false;
		StringBuffer sql = new StringBuffer();
		for (String column : columns) {			sql.append(",`"+column+"` = ? ");		}
		sql.deleteCharAt(0);
		sql.insert(0, "UPDATE `BlogSpot` SET ");
		sql.append("WHERE TRUE AND `blogSpotId` = ? ");
		PreparedStatement pstmt=conn.prepareStatement(sql.toString());
		for (int i=0;i<columns.size();i++) {
			String column = columns.elementAt(i);
			pstmt.setObject(i+1,blogSpot.get(column));
		}
		pstmt.setObject(columns.size()+1+0,blogSpot.get("blogSpotId"));
		int affected=pstmt.executeUpdate();
		return affected>0;
	}
	public static ResultSet insert(BlogSpot blogSpot, Connection conn) throws SQLException {
		StringBuffer sql1 = new StringBuffer();
		StringBuffer sql2 = new StringBuffer();
		for (String column : columnNames) {
			if (blogSpot.get(column) != null) {
				sql1.append(",`" + column+"`");
				sql2.append(", ? ");
			}
		}
		sql1.deleteCharAt(0);
		sql2.deleteCharAt(0);
		String sql = "INSERT INTO `BlogSpot` (" + sql1.toString() + ") VALUES (" + sql2.toString() + ") ";
		System.out.println(sql);
		PreparedStatement pstmt = conn.prepareStatement(sql);
		for (int i = 0, j = 0; i < columnNames.length; i++) {
			String column = columnNames[i];
			System.out.println(column);
			if (blogSpot.get(column) != null) {
				pstmt.setObject(i + 1 + j, blogSpot.get(column));
			}else{
				j--;
			}
		}
		int affected=pstmt.executeUpdate();
		return pstmt.getGeneratedKeys();
	}
	public static Collection<BlogSpot> getBlogSpotCollection(String conditions, Object[] values, int offset, int limit, Connection conn) throws SQLException {
		ResultSet rs=BasePeer.executeQuery(tableName,columnNames,conditions,values, offset,  limit,conn);
		Collection<BlogSpot> collection=new Vector<BlogSpot>();
		fill(rs,collection);
		return collection;
	}
}
